package com.dengjunbo.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dengjunbo.commonutils.JwtUtils;
import com.dengjunbo.commonutils.MD5;
import com.dengjunbo.educenter.entity.UcenterMember;
import com.dengjunbo.educenter.entity.vo.RegisterVo;
import com.dengjunbo.educenter.mapper.UcenterMemberMapper;
import com.dengjunbo.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dengjunbo.servicebase.exceptionhanler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author dengjunbo
 * @since 2020-04-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"账号密码不能为空");
        }

        QueryWrapper<UcenterMember> wrapper =new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember == null){
            throw new GuliException(20001,"没有该用户");
        }
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        if(mobileMember.getIsDisabled()){
            throw new GuliException(20001,"用户已禁用");
        }
        String token = JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)||StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"数据不能为空");
        }
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!redisCode.equals(code)){
            throw new GuliException(20001,"验证码错误");
        }

        QueryWrapper<UcenterMember>wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new GuliException(20001,"用户已存在");
        }

        //用户添加到数据库中
        UcenterMember member =new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://dengjunbo-edu.oss-cn-beijing.aliyuncs.com/2020/04/26/8c0f7816d4044b0984d1801825192064file.png");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember>wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

}
